import serial
from time import sleep


class Device:
    DEFAULTS = {'write_termination': '\n', 'read_termination': '\n', 'encoding': 'ascii', 'baudrate': 9600,
                'read_timeout': 1, 'write_timeout': 1}
    def __init__(self, port):
        self.port = port
        self.rsc = None

    def initialize(self):
        self.rsc = serial.Serial(port=self.port,
                                 baudrate=self.DEFAULTS['baudrate'],
                                 timeout=self.DEFAULTS['read_timeout'],
                                 write_timeout=self.DEFAULTS['write_timeout'])
        sleep(1)

    def query(self, message):
        message = message + self.DEFAULTS['write_termination']
        message = message.encode(self.DEFAULTS['encoding'])
        #print(message)
        self.rsc.write(message)
        ans = self.rsc.readline()
        ans = ans.decode(self.DEFAULTS['encoding']).strip()
        return ans

    def idn(self,message):
        return self.query(message)

dev = Device('COM14') #<---- Remember to change the port
dev.initialize()
while(1):
    serial_number = dev.idn('I am a big man')
    print(f'The device serial number is: {serial_number}')
    sleep(1)

